// Fill out your copyright notice in the Description page of Project Settings.


#include "CalendarWidget.h"
#include "SCalendarWidget.h"
#include "SMyComboButton.h"
#include "TimeHelper.h"
#include "ListLibrary/ListUIStyle.h"
#include "Widgets/Images/SImage.h"
#include "Styling/SlateBrush.h"

static FButtonStyle* DefaultButtonStyle = nullptr;
static FSlateBrush* ImageStyle;
UCalendarWidget::UCalendarWidget()
{

	if (DefaultButtonStyle == nullptr)//默认按钮样式
	{
		DefaultButtonStyle = new FButtonStyle(FCoreStyle::Get().GetWidgetStyle<FButtonStyle>("Button"));
		DefaultButtonStyle->UnlinkColors();
	}
	WidgetStyle = *DefaultButtonStyle;

	if (!IsRunningDedicatedServer())//默认文本样式
	{
		TextStyle = FCoreStyle::Get().GetWidgetStyle<FTextBlockStyle>("NormalText");
	}
	//if (MyStyle::UIStyle)
	//{
	if (!IsRunningDedicatedServer())//默认图片样式
	{
		//获取系统自定义样式,只能从FCoreStyle::Get()中获取
		ImageStyle = new FSlateBrush(FCoreStyle::Get().GetWidgetStyle< FComboButtonStyle >("ComboButton").DownArrowImage);
		//ImageStyle = const_cast<FSlateBrush*>(&MyStyle::UIStyle->CalendarImage);//获取自定义的图片获取失败
		SlateBrush = *ImageStyle;
		ImageStyle->UnlinkColors();
	}
	CalendarTime = GanttTimeHelper::GetTodayZero();//默认获取今天的时间
}

//UMG列表栏
const FText UCalendarWidget::GetPaletteCategory()
{
	return FText::FromString(TEXT("小辉辉"));
}

//资源释放
void UCalendarWidget::ReleaseSlateResources(bool bReleaseChildren)
{
	Super::ReleaseSlateResources(bReleaseChildren);
	MyComboButton.Reset();
	PlayCalendar.Reset();
	DateText.Reset();
	
}

//部件的详细结构
TSharedRef<SWidget> UCalendarWidget::RebuildWidget()
{
	return
		SAssignNew(MyComboButton, SMyComboButton)
		.ButtonStyle(ListUIStyle::GetButtonStyle("transparencyStyle"))
		.ButtonContent()
		[
			SAssignNew(DateText, STextBlock)//设置显示的文本
			.Text_Lambda([this]() -> FText {
				return HintText;
			})
		]
		.MenuContent()
		[
			SAssignNew(PlayCalendar, SCalendarWidget)
			.InDateTime(&CalendarTime)
			.OnUpdateData_Lambda([this](int year, int month, int day) {//当选中时间时会触发此事件
				UpdataCalendar(year, month, day);
			})
		]
		.OnComboBoxOpened_Lambda([this]() {//comboButton时会更新日历时间
			//打开菜单时修改日历的日期为当前日期
			PlayCalendar->Year = GanttTimeHelper::GetYear(CalendarTime);
			PlayCalendar->Month = GanttTimeHelper::GetMonth(CalendarTime);
			PlayCalendar->UpdataButtonsState();
		});
}

//更新时间
void UCalendarWidget::UpdataCalendar(int year, int month, int day)
{
	if (GanttTimeHelper::DateToTimestamp(year, month, day) >= 0)
	{
		CalendarTime = GanttTimeHelper::DateToTimestamp(year, month, day);
		HintText = FText::FromString(GanttTimeHelper::UnixTimestampToStr(CalendarTime));
		DateText->SetText(HintText);
		SynchronizeProperties();
		OnDateUpdata.Broadcast();//触发事件
	}
}

//设置时间
void UCalendarWidget::SetTime(int year, int month, int day)
{
	UpdataCalendar(year, month, day);
}

//获取时间
void UCalendarWidget::GetTime(int& year, int& month, int& day)
{
	GanttTimeHelper::TimestampToData(CalendarTime, year, month, day);
}

//同步属性
void UCalendarWidget::SynchronizeProperties()
{
	Super::SynchronizeProperties();
	DateText->SetTextStyle(&TextStyle);
}
